package com.example.androidhive;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
//import com.example.androidhive.Logowanie.CreateNewProduct1;
//import com.techblogon.serviceexample.MyService;

public class MojeDane extends Activity {
	private CheckBox chkIos, chkAndroid, chkWindows;      ListView listView ;
	  private Button btnDisplay;
	  
		private ProgressDialog pDialog;private ListView listView1;
		MediaPlayer player;
		JSONParser jsonParser = new JSONParser();
		EditText inputName;
		EditText inputPrice;
		EditText inputDesc;
		Set<Pokoj> pokoje=new HashSet<Pokoj>();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my);
		addListenerOnChkIos();
		addListenerOnButton(); 
		//odczytaj();
		JSONObject a =Wynik.json;String h="";
		try {
			h=null;
			h =(String) a.get("obiekty_stany").toString();
		} catch (JSONException e) {
			e.printStackTrace();
		}
			h=h.replace("},{", "!");
			h=h.replace("{", "");
			h=h.replace("}", "");
			h=h.replace("[", "");
			h=h.replace("]", "");
			String str =h;
			String delimiter = "!";
			String[] temp;
			temp = str.split(delimiter);
			for(String _temp:temp)
				pokoje.add(new Pokoj(_temp));
			String[] values = new String[] {};
			List<String> cc=new ArrayList<String>();
			for (Pokoj aa:pokoje) {
				cc.add(aa._id_obiektu);
			}
			String[] avalues=new String[cc.size()];
			for (int i = 0; i < avalues.length; i++) {
				avalues[i]="aa"+i;
			}
		chkIos = (CheckBox) findViewById(R.id.chkIos);
		chkIos.setChecked(false);
	    listView = (ListView) findViewById(R.id.lista);
     /*   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, android.R.id.text1, avalues);
*/
	    /*Weather weather_data[] = new Weather[]
	            {
	                new Weather( "Cloudy"),
	                new Weather( "Showers"),
	                new Weather("Snow"),
	                new Weather("Storm"),
	                new Weather("Sunny")
	            };
	            WeatherAdapter adapter = new WeatherAdapter(this,
	                    R.layout.listview_item_row, weather_data);
	            listView1 = (ListView)findViewById(R.id.lista);
	            View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
	            listView1.addHeaderView(header);
	            listView1.setAdapter(adapter);
	    */
        /*listView.setAdapter(adapter); 
        
        listView.setOnItemClickListener(new OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> parent, View view,
                 int position, long id) {
               int itemPosition     = position;
               String  itemValue    = (String) listView.getItemAtPosition(position);
                Toast.makeText(getApplicationContext(),
                  "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
                  .show();
              }
         });*/ 
		
		//String v=h;
		
		
		//odczyt danych z web
		//setVolumeControlStream(AudioManager.STREAM_RING);
        //we now instantiate the player using our music file;
        //note that since it is a resource in the "raw" directory,
        //we don't need the file's extension;
        //our file here is in fact called "soundfile.mp3";
		
	
      //  player = MediaPlayer.create(this, R.raw.eee);
        //we start playing the file!
      //  player.start();
	}
	
	
	private class ItemBO{
		 private String name;
		    private String description;
		    public String getName() { return name;  }
		    public void setName(String name) { this.name = name; }
		    public String getDescription() { return description; }
		    public void setDescription(String description) { this.description = description; }
		    // / --------------------
		    public ArrayList getItems() {
		        ArrayList list = new ArrayList();
		        ItemBO item;
		 
		        item = new ItemBO();
		        item.setName("item 1");
		        item.setDescription("desc 1");
		        list.add(item);
		 
		        item = new ItemBO();
		        item.setName("item 2");
		        item.setDescription("desc 2");
		        list.add(item);
		 
		        item = new ItemBO();
		        item.setName("item 3");
		        item.setDescription("desc 3");
		        list.add(item);
		 
		        return list;
		    }
	}
	  // ***ListAdapter***
	 private class StableArrayAdapter extends ArrayAdapter<String> {

		    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

		    public StableArrayAdapter(Context context, int textViewResourceId,
		        List<String> objects) {
		      super(context, textViewResourceId, objects);
		      for (int i = 0; i < objects.size(); ++i) {
		        mIdMap.put(objects.get(i), i);
		      }
		    }

		    @Override
		    public long getItemId(int position) {
		      String item = getItem(position);
		      return mIdMap.get(item);
		    }

		    @Override
		    public boolean hasStableIds() {
		      return true;
		    }

		  }
	void odczytaj(){
		Map<String,String> mma=new HashMap<String,String>();
		mma.put("nazwa", "mm");
		mma.put("sprawdz", "mm");
		mma.put("id_klient", "3");
		mma.put("id", "3");
		mma.put("pobieranie", "_");
		Base_ aao=new Base_(MojeDane.this,mma,getApplicationContext());
		aao.setAaa(new String[]{"wyposarzenie"});
		aao.execute();	
	}
	 public void addListenerOnChkIos() {
		
			chkIos = (CheckBox) findViewById(R.id.chkIos);
			chkIos.setOnClickListener(new OnClickListener() {
		 
			  @Override
			  public void onClick(View v) {
					
		                //is chkIos checked?
				/*if (((CheckBox) v).isChecked()) {
					Toast.makeText(MojeDane.this,
				 	   "Bro, try Android :)", Toast.LENGTH_LONG).show();
				}*/
		 
			  }
			});
		 
		  }
		 
		  public void addListenerOnButton() {
		 
			chkIos = (CheckBox) findViewById(R.id.chkIos);
			chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
			chkWindows = (CheckBox) findViewById(R.id.chkWindows);
			btnDisplay = (Button) findViewById(R.id.btnDisplay);
		 
			btnDisplay.setOnClickListener(new OnClickListener() {
		 
		          //Run when button is clicked
			  @Override
			  public void onClick(View v) {
		 
				StringBuffer result = new StringBuffer();
				result.append("IPhone check : ").append(chkIos.isChecked());
				result.append("\nAndroid check : ").append(chkAndroid.isChecked());
				result.append("\nWindows Mobile check :").append(chkWindows.isChecked());
				String[] tablica={"prysznic",(chkIos.isChecked())?"1":"0"};
				Toast.makeText(MojeDane.this, result.toString(),
						Toast.LENGTH_LONG).show();
			//	rr();
				mm.put("prysznic",(chkIos.isChecked())?"1":"0");
				mm.put("tv", "1");
						mm.put("wifi", "1");
								mm.put("suszarka","1");
										mm.put("pralka", "1");
				//putIntoArrayAndSave(tablica);
				putIntoArrayAndSave(mm);
			  }
			});
		 
		  }
		  Map<String,String> mm=new HashMap<String,String>();
		  //void putIntoArrayAndSave(String[] a){
			  void putIntoArrayAndSave(Map<String,String> a){
Map<String,String> mma=new HashMap<String,String>();
mma.put("nazwa", "mm");mma.putAll(a);
				String[] c={"nazwa", "mm"
				,"tv", "1","wifi", "1","suszarka","1",
						"pralka", "1"};
				Base_ aa=new Base_(MojeDane.this,mma,getApplicationContext());
				aa.setAaa(new String[]{"wyposarzenie"});
				aa.execute();			  
		  }
		  void rr(){
			  String youraddress="Kluczbork Jana Pawla II";
			  String uri = "http://maps.google.com/maps/api/geocode/json?address=Dąbrowa,Cherubiny 22&sensor=false";
	    HttpGet httpGet = new HttpGet(uri);
	    HttpClient client = new DefaultHttpClient();
	    HttpResponse response;
	    StringBuilder stringBuilder = new StringBuilder();

	    try {
	        response = client.execute(httpGet);
	        HttpEntity entity = response.getEntity();
	        InputStream stream = entity.getContent();
	        int b;
	        while ((b = stream.read()) != -1) {
	            stringBuilder.append((char) b);
	        }
	    } catch (ClientProtocolException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }

	    JSONObject jsonObject = new JSONObject();
	    try {
	        jsonObject = new JSONObject(stringBuilder.toString());
	        double lat= 0.0, lng= 0.0;
	        lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
	            .getJSONObject("geometry").getJSONObject("location")
	            .getDouble("lng");

	        lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
	            .getJSONObject("geometry").getJSONObject("location")
	            .getDouble("lat");
	    	Toast.makeText(MojeDane.this, lat+"-"+lng,
					Toast.LENGTH_LONG).show();
	        //Log.d("latitude", lat);
	        //Log.d("longitude", lng);
	    } catch (JSONException e) {
	        e.printStackTrace();
	    }
		      
	}

}